package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.GwbfT;
import cn.e4j.modular.crm.entity.GwbflT;
import cn.e4j.modular.crm.mapper.GwbfTMapper;
import cn.e4j.modular.crm.service.GwbfTService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  拜访内容设置 服务实现类
 * </p>
 *
 * @author mengyb
 * @since 2019-04-12
 */
@Service
public class GwbfTServiceImpl extends ServiceImpl<GwbfTMapper,GwbfT> implements GwbfTService {

    @Transactional(readOnly = true)
    @Override
    public List<Map<String, Object>> getList(Dto dto, Page<Object> page) {

        ShiroUser shiroUser = ShiroKit.getUser();

        dto.put("ent", shiroUser.getDBC());
        dto.put("lang", LangUtils.getLang(shiroUser.getLANG()));

        return this.baseMapper.getList(dto, page);

    }

    @Override
    public List<Map<String, Object>> getOrgsList(Dto dto) {
        return this.baseMapper.getOrgsList(dto);
    }

    @Transactional
    @Override
    public void add(GwbfT gwbfT, GwbflT gwbflT) {

        ShiroUser shiroUser = ShiroKit.getUser();
        gwbfT.setGWBFENT(shiroUser.getDBC());

        gwbfT.setGWBF003(" ");
        gwbfT.setGWBF004(" ");
        gwbfT.setGWBF005(" ");
        gwbfT.setGWBFDATE(LocalDate.now());
        gwbfT.setGWBFTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
        gwbfT.setGWBFUSER(shiroUser.getACCOUNT());
        gwbfT.setGWBFGRUP(shiroUser.getORG_ID());
        gwbfT.setGWBFORIU(shiroUser.getACCOUNT());
        gwbfT.setGWBFORIG(shiroUser.getORG_ID());

        gwbflT.setGWBFLENT(shiroUser.getDBC());
        gwbflT.setGWBFL001(gwbfT.getGWBF001());
        gwbflT.setGWBFL002(gwbfT.getGWBF002());
        gwbflT.setGWBFL003(LangUtils.getLang(shiroUser.getLANG()));

        gwbfT.insert();
        gwbflT.insert();
    }

    @Transactional
    @Override
    public void edit(GwbfT gwbfT, GwbflT gwbflT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwbfT.setGWBFMODU(shiroUser.getACCOUNT());

        UpdateWrapper<GwbfT> gwbfTUpdateWrapper = new UpdateWrapper<>();
        gwbfTUpdateWrapper.eq("GWBFENT",gwbfT.getGWBFENT());
        gwbfTUpdateWrapper.eq("GWBF001",gwbfT.getGWBF001());
        gwbfTUpdateWrapper.eq("GWBF002",gwbfT.getGWBF002());


        UpdateWrapper<GwbflT> gwbflTUpdateWrapper = new UpdateWrapper<>();
        gwbflTUpdateWrapper.eq("GWBFLENT", gwbfT.getGWBFENT());
        gwbflTUpdateWrapper.eq("GWBFL001", gwbfT.getGWBF001());
        gwbflTUpdateWrapper.eq("GWBFL002", gwbfT.getGWBF002());

        gwbfT.update(gwbfTUpdateWrapper);
        gwbflT.update(gwbflTUpdateWrapper);

    }

    @Transactional
    @Override
    public void delRow(GwbfT gwbfT, GwbflT gwbflT) {

        UpdateWrapper<GwbfT> gwbfTUpdateWrapper = new UpdateWrapper<>();
        gwbfTUpdateWrapper.eq("GWBFENT", gwbfT.getGWBFENT());
        gwbfTUpdateWrapper.eq("GWBF001", gwbfT.getGWBF001());
        gwbfTUpdateWrapper.eq("GWBF002",gwbfT.getGWBF002());

        UpdateWrapper<GwbflT> gwbflTUpdateWrapper = new UpdateWrapper<>();
        gwbflTUpdateWrapper.eq("GWBFLENT", gwbfT.getGWBFENT());
        gwbflTUpdateWrapper.eq("GWBFL001", gwbfT.getGWBF001());
        gwbflTUpdateWrapper.eq("GWBFL002", gwbfT.getGWBF002());

        gwbfT.delete(gwbfTUpdateWrapper);
        gwbflT.delete(gwbflTUpdateWrapper);

    }

    @Transactional
    @Override
    public void delRows(List<GwbfT> gwbfTList) {
        for (GwbfT gwbfT : gwbfTList) {
            UpdateWrapper<GwbfT> gwbfTUpdateWrapper = new UpdateWrapper<>();
            gwbfTUpdateWrapper.eq("GWBFENT", gwbfT.getGWBFENT());
            gwbfTUpdateWrapper.eq("GWBF001", gwbfT.getGWBF001());
            gwbfTUpdateWrapper.eq("GWBF002",gwbfT.getGWBF002());

            GwbflT gwbflT=new GwbflT();
            UpdateWrapper<GwbflT> gwbflTUpdateWrapper = new UpdateWrapper<>();
            gwbflTUpdateWrapper.eq("GWBFLENT", gwbfT.getGWBFENT());
            gwbflTUpdateWrapper.eq("GWBFL001", gwbfT.getGWBF001());
            gwbflTUpdateWrapper.eq("GWBFL002", gwbfT.getGWBF002());

            gwbfT.delete(gwbfTUpdateWrapper);
            gwbflT.delete(gwbflTUpdateWrapper);
        }
    }

    /**
     * 查重
     * @param gwbfT
     * @return
     */
    @Transactional(readOnly = true)
    @Override
    public Boolean existedTheParams(GwbfT gwbfT) {
        GwbfT gwbfT1=this.baseMapper.getByKey(gwbfT.getGWBFENT(),gwbfT.getGWBF001(),gwbfT.getGWBF002());

        return gwbfT1!=null?true:false;
    }
}
